package signIn;

import view.GameFrame;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;

public class SignIn extends JFrame implements ActionListener {
    JTextField usernameText = new JTextField(20);//用户名
    JTextField passwordField = new JTextField(20); //密码
    JTextField confirmPasswordField = new JTextField(20);  //确认密码

    public SignIn() {
        //初始化界面
        InitFrame();
        //初始化菜单
        InitMenu();
        //显示
        this.setVisible(true);
    }

    //初始换界面
    public void InitFrame() {
        //1.设置界面的大小
        this.setSize(350, 200);
        //2.设置界面的标题
        this.setTitle("游戏注册 ");
        //3.设置界面置顶
        this.setAlwaysOnTop(true);
        //4.设置界面居中
        this.setLocationRelativeTo(null);
        //5.设置关闭模式
        this.setDefaultCloseOperation(2);
    }

    //初始化菜单
    public void InitMenu() {
        this.setLayout(null);
        //创建用户名
        JLabel username = new JLabel("用户名:");
        username.setBounds(10, 20, 80, 25);
        this.add(username);

        usernameText.setBounds(100, 20, 165, 25);
        this.add(usernameText);

        //创建密码
        JLabel password = new JLabel("密码:");
        password.setBounds(10, 50, 80, 25);
        this.add(password);

        passwordField.setBounds(100, 50, 165, 25);
        this.add(passwordField);

        //创建确认密码
        JLabel confirmPassword = new JLabel("确认密码");
        confirmPassword.setBounds(10, 80, 80, 25);
        this.add(confirmPassword);

        confirmPasswordField.setBounds(100, 80, 165, 25);
        this.add(confirmPasswordField);

        //创建注册按钮
        JButton registerButton = new JButton("注册");
        registerButton.setBounds(140, 120, 80, 25);
        registerButton.addActionListener(this);
        this.add(registerButton);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String username = usernameText.getText();
        String password = new String(passwordField.getText());
        String confirmPassword = new String(confirmPasswordField.getText());
        if (password.equals(confirmPassword)) {

        } else {
            JOptionPane.showMessageDialog(this, "                    两次密码输入不一致","出错啦！",JOptionPane.PLAIN_MESSAGE);
            return;
        }
        if (getUsernameFromFile("UserInfo.txt", username)) {
            JOptionPane.showMessageDialog(this, "              用户名已存在，请更换用户名！","出错啦！",JOptionPane.PLAIN_MESSAGE);
        } else {
            try {
                addFile(username, password, confirmPassword);
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
            //弹出用户信息
            JOptionPane.showMessageDialog(this, "                               注册成功\n\n" +
                    "                            用户名：" + username + "\n" +
                    "                            密码：" + password + "\n","注册",JOptionPane.PLAIN_MESSAGE);
             setVisible(false);
               new GameFrame();

        }


    }

    public boolean getUsernameFromFile(String filePath, String name) {
        try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = br.readLine()) != null) {
                String[] parts = line.split(";");
                if (parts.length > 0) {
                    if (parts[0].equals(name)) {
                        return true;
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    public void addFile(String username, String password, String confirmPassword) throws IOException {
        FileWriter fileWriter = new FileWriter("UserInfo.txt", true); // 设置为true表示追加写入，而不是覆盖原有内容
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
        String userInfo = String.format("%s;%s;%s", username, password, confirmPassword);
        bufferedWriter.write(userInfo);
        bufferedWriter.newLine(); // 添加换行符，使每个用户信息占一行
        bufferedWriter.flush(); // 立即将缓冲区数据写入文件
        bufferedWriter.close();
        fileWriter.close();
    }
}
